﻿using EasyModbus;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace PLC_Master
{
    /// <summary>
    /// S7-200 Smart SR30 
    /// </summary>
    public class PLCMaster
    {
        private static readonly object InstanceLocker = new object();
        private static PLCMaster _Instance = null;
        private ModbusClient _modbusClient;

        private PLCMaster()
        {
            //私有化构造方法，禁止创建新的对象
        }

        /// <summary>
        /// 获取实例对象
        /// </summary>
        /// <returns></returns>
        public static PLCMaster GetInstance()
        {
            lock (InstanceLocker)
            {
                if (_Instance == null)
                {
                    _Instance = new PLCMaster();
                }
            }

            return _Instance;
        }

        /// <summary>
        /// 查询连接状态
        /// </summary>
        public bool Connected
        {
            get
            {
                if (_modbusClient != null && _modbusClient.Connected)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }

        /// <summary>
        /// 连接
        /// </summary>
        /// <param name="IP">地址</param>
        /// <param name="Port">端口</param>
        public bool Connect(string IP, int Port)
        {
            if (Connected)
            {
                throw new Exception("已经连接");
            }

            try
            {
                _modbusClient = new ModbusClient(IP, Port);
                _modbusClient.Connect();
                return true;
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
                return false;
            }
        }

        public void Disconnect()
        {
            if (_modbusClient != null)
            {
                try
                {
                    _modbusClient.Disconnect();
                }
                finally
                {
                    _modbusClient = null;
                }
            }
        }


        /// <summary>
        /// 读取最新状态
        /// </summary>
        public PLCStatus ReadStatus()
        {
            if (Connected)
            {
                PLCStatus status = new PLCStatus();
                int[] words = _modbusClient.ReadHoldingRegisters(0, 10);

                //状态
                byte VB0 = (byte)(words[0] >> 8);
                byte VB1 = (byte)(words[00] & 0x00FF);
                status.IsEmergencyStop = (VB0 & 0x01) == 1;

                return status;
            }
            else
            {
                return null;
            }
        }


        public void StartTest()
        {
            //V2.0
            _modbusClient.WriteSingleRegister(1, 0x0100);
        }

        public void StopTest()
        {
            //V2.0
            _modbusClient.WriteSingleRegister(1, 0x0000);
        }
    }
}
